package org.nowcoder.leetcode.twoPointers;

/**
 * Title  : 234. Palindrome Linked List
 * Source : https://leetcode.cn/problems/palindrome-linked-list/description/
 * Author : XrazYang
 * Date   : 2023-10-21
 */

public class LeetCode_234 {
    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode p = head;
        StringBuilder builder = new StringBuilder();
        while (p != null) {
            builder.append(p.val);
            p = p.next;
        }
        String s = builder.toString();
        String s1 = builder.reverse().toString();
        return s.equals(s1);
    }
}
